package me.ezitku.base.extensions.PopupWindow

import android.content.Context
import androidx.fragment.app.Fragment
import android.view.*
import android.widget.PopupWindow

/**
 * Created by yurqi on 2017/5/12.
 */


class SPopup(var context: Context, layoutId: Int) : PopupWindow() {
    var rootView: View = LayoutInflater.from(context).inflate(layoutId, null)
    var mWidth = 300
    var mHeight = ViewGroup.LayoutParams.WRAP_CONTENT
    var mGravity = Gravity.CENTER

    fun init() {
        isOutsideTouchable = false
        isFocusable = false
        contentView = rootView

        this.width = mWidth
        this.height = mHeight
//        rootView.isFocusable = true
//        rootView.setOnKeyListener { view, keycode, keyEvent ->
//            if (keycode == KeyEvent.KEYCODE_BACK)
//                if (isShowing) {
//                    dismiss()
//                }
//            return@setOnKeyListener false
//        }

        update()
    }

}

fun Context.showPopup(resId: Int, action: SPopup.() -> Unit): PopupWindow {
    var popup = SPopup(this, resId)
    action(popup)
    popup.init()
    return popup
}

fun Fragment.showPopup(resId: Int, action: SPopup.() -> Unit): PopupWindow {
    return activity!!.showPopup(resId, action)
}


fun PopupWindow?.dismisCheckNull(): Boolean {
    if (this != null && this.isShowing) {
        this.dismiss()
        return true
    } else {
        return false
    }

}

fun Array<PopupWindow?>.isNotNull(notNull: (Boolean) -> Unit) {
    var res = false
    forEach {
        res = it != null
    }
    notNull(res)
}


